package com.bwie.agent.service.impl;

import com.bwie.agent.config.JwtUtil;
import com.bwie.agent.dao.RoleDao;
import com.bwie.agent.dao.UsersAndRolesDao;
import com.bwie.agent.entity.RoleEntity;
import com.bwie.agent.entity.UsersAndRolesEntity;
import com.bwie.agent.service.UsersAndRolesService;
import com.bwie.common.utils.Md5Utils;
import com.bwie.common.utils.R;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.net.URL;
import java.net.URLDecoder;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.common.utils.PageUtils;
import com.bwie.common.utils.Query;

import com.bwie.agent.dao.UserDao;
import com.bwie.agent.entity.UserEntity;
import com.bwie.agent.service.UserService;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;


@Service("userService")
public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {

    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Autowired
    UserDao userDao;

    @Autowired
    UsersAndRolesDao usersAndRolesDao;

    @Autowired
    JwtUtil jwtUtil;

    @Autowired
    RoleDao roleDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<UserEntity> page = this.page(
                new Query<UserEntity>().getPage(params),
                new QueryWrapper<UserEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public R login(UserEntity user) {
        UserEntity userEntity = userDao.selectOne(new QueryWrapper<UserEntity>().eq("user_nike_name", user.getUserNikeName()).eq("user_pwd", Md5Utils.md5(user.getUserPwd())));
        if (userEntity!=null){
            UsersAndRolesEntity user_id = usersAndRolesDao.selectOne(new QueryWrapper<UsersAndRolesEntity>().eq("user_id", userEntity.getUserId()));
            RoleEntity roleEntity = roleDao.selectById(user_id.getRoleId());
            String jwt = jwtUtil.createJWT(userEntity.getUserId().toString(), userEntity.getUserNikeName(), roleEntity.getRoleName());
            return R.ok().put("token", jwt);
        }
        return R.error("请先注册");
    }

    /**
     * 用户注册 默认注册为普通用户 申请为经纪人再修改中间表
     * */
    @Override
    public R register(UserEntity user) {
        String s = stringRedisTemplate.opsForValue().get(user.getUserTel());
        if (StringUtils.isBlank(user.getCode()) || !user.getCode().equals(s))
            return R.error("验证码错误");
        user.setUserPwd(Md5Utils.md5(user.getUserPwd()));
        userDao.insert(user);
        UsersAndRolesEntity usersAndRolesEntity = new UsersAndRolesEntity();
        usersAndRolesEntity.setUserId(user.getUserId());
        usersAndRolesEntity.setRoleId(1);
        int insert1 = usersAndRolesDao.insert(usersAndRolesEntity);
        return insert1==1?R.ok():R.error();
    }

}